/**
 * 
 */
package game.kill.web.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import game.kill.biz.GameBiz;
import game.kill.biz.RoomBiz;
import game.kill.enums.PlayerActionEnum;
import game.kill.enums.PlayerRoleEnum;
import game.kill.enums.RoomActionEnum;
import game.kill.exception.KillException;
import game.kill.exception.KillException.KillErrorCode;
import game.kill.model.GameRoomInfo;
import game.kill.model.PlayerAction;
import game.kill.model.Room;
import game.kill.model.RoomUser;
import game.kill.service.RoomUserService;
import game.kill.service.UserService;

/**
 * @author leiruiqi
 *
 */
@RequestMapping("/test")
@Controller
public class TestController {

	@Resource
	private UserService userService;
	
	@Resource
	private RoomUserService roomUserService;
	
	
	@Resource
	private GameBiz gameBiz;
	
	@Resource
	private RoomBiz roomBiz;
	

	
	
	@RequestMapping("/testa")
	@ResponseBody
	public String testa(){
		return "testa";
	}
	
	@RequestMapping("/index")
	public String index(){
		//userService.g
		return "test/index";
	}
	
	@RequestMapping("/createRoom")
	@ResponseBody
	public Room createRoom(@RequestParam Long userId,@RequestParam Integer playerNum){
		Room room = roomBiz.createRoom(userId, playerNum);
		return room;
	} 
	
	@RequestMapping("/room/in")
	@ResponseBody
	public RoomUser roomIn(@RequestParam Long userId,@RequestParam Long roomId){
		
		roomBiz.doRoomAction(userId, roomId, RoomActionEnum.in);
		RoomUser roomUser = roomUserService.getByRoomAndUser(roomId, userId);
		return roomUser;
	} 
	
	@RequestMapping("/action/kill")
	@ResponseBody
	public String actionKill(@RequestParam Long userId,@RequestParam Long roomId,@RequestParam String targetInfo){
		
		GameRoomInfo gameRoomInfo = roomBiz.getUserRoomInfo(roomId);
		RoomUser roomUser = gameRoomInfo.findByUserId(userId);
		if(roomUser == null){
			throw new KillException(KillErrorCode.PARAM_ERROR,"not member of room"); 
		}
		PlayerAction playerAction = new PlayerAction();
		playerAction.setRoomUser(roomUser);
		playerAction.setActionEnum(PlayerActionEnum.kill);
		playerAction.setRoleEnum(PlayerRoleEnum.getByCode(roomUser.getRoleId()));
		playerAction.setTargetInfo(targetInfo);
		gameBiz.gamePlayAction(playerAction, gameRoomInfo);
		
		return "ok";
	} 
	
	@RequestMapping("/action/check")
	@ResponseBody
	public String actionCheck(@RequestParam Long userId,@RequestParam Long roomId,@RequestParam String targetInfo){

		GameRoomInfo gameRoomInfo = roomBiz.getUserRoomInfo(roomId);
		RoomUser roomUser = gameRoomInfo.findByUserId(userId);
		if(roomUser == null){
			throw new KillException(KillErrorCode.PARAM_ERROR,"not member of room"); 
		}
		PlayerAction playerAction = new PlayerAction();
		playerAction.setRoomUser(roomUser);
		playerAction.setActionEnum(PlayerActionEnum.check);
		playerAction.setRoleEnum(PlayerRoleEnum.getByCode(roomUser.getRoleId()));
		playerAction.setTargetInfo(targetInfo);
		gameBiz.gamePlayAction(playerAction, gameRoomInfo);
		
		return "ok";
	} 
	
	@RequestMapping("/action/discuss")
	@ResponseBody
	public String actionDiscuss(@RequestParam Long userId,@RequestParam Long roomId,@RequestParam String targetInfo){
		GameRoomInfo gameRoomInfo = roomBiz.getUserRoomInfo(roomId);
		RoomUser roomUser = gameRoomInfo.findByUserId(userId);
		if(roomUser == null){
			throw new KillException(KillErrorCode.PARAM_ERROR,"not member of room"); 
		}
		PlayerAction playerAction = new PlayerAction();
		playerAction.setRoomUser(roomUser);
		playerAction.setActionEnum(PlayerActionEnum.discuss);
		playerAction.setRoleEnum(PlayerRoleEnum.getByCode(roomUser.getRoleId()));
		playerAction.setTargetInfo(targetInfo);
		gameBiz.gamePlayAction(playerAction, gameRoomInfo);
		
		return "ok";
	} 
	
	@RequestMapping("/action/speak")
	@ResponseBody
	public String actionSpeak(@RequestParam Long userId,@RequestParam Long roomId,@RequestParam String targetInfo){
		GameRoomInfo gameRoomInfo = roomBiz.getUserRoomInfo(roomId);
		RoomUser roomUser = gameRoomInfo.findByUserId(userId);
		if(roomUser == null){
			throw new KillException(KillErrorCode.PARAM_ERROR,"not member of room"); 
		}
		PlayerAction playerAction = new PlayerAction();
		playerAction.setRoomUser(roomUser);
		playerAction.setActionEnum(PlayerActionEnum.speak);
		playerAction.setRoleEnum(PlayerRoleEnum.getByCode(roomUser.getRoleId()));
		playerAction.setTargetInfo(targetInfo);
		gameBiz.gamePlayAction(playerAction, gameRoomInfo);
		
		return "ok";
	} 
	
	@RequestMapping("/action/vote")
	@ResponseBody
	public String actionVotek(@RequestParam Long userId,@RequestParam Long roomId,@RequestParam String targetInfo){
		GameRoomInfo gameRoomInfo = roomBiz.getUserRoomInfo(roomId);
		RoomUser roomUser = gameRoomInfo.findByUserId(userId);
		if(roomUser == null){
			throw new KillException(KillErrorCode.PARAM_ERROR,"not member of room"); 
		}
		PlayerAction playerAction = new PlayerAction();
		playerAction.setRoomUser(roomUser);
		playerAction.setActionEnum(PlayerActionEnum.vote);
		playerAction.setRoleEnum(PlayerRoleEnum.getByCode(roomUser.getRoleId()));
		playerAction.setTargetInfo(targetInfo);
		gameBiz.gamePlayAction(playerAction, gameRoomInfo);
		
		return "ok";
	} 
}
